package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.guesstv.lib.module.GtDataProvider;
import com.stark.guesstv.lib.module.bean.TvActorBean;
import com.stark.guesstv.lib.module.dongman.DmBean;
import com.stark.guesstv.lib.module.dongman.DmDataProvider;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityGameBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.RandomUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.view.container.StkRelativeLayout;

public class GameActivity extends BaseAc<ActivityGameBinding> {

    //看图猜角色
    private int level = 0;
    private List<DmBean> listAll = new ArrayList<>();
    private List<String> listAnswer = new ArrayList<>();
    private int rightNum;

    @Override
    protected int onCreate() {
        return R.layout.activity_game;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);
        mDataBinding.ivGameBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mDataBinding.rlGameOptionA.setOnClickListener(this);
        mDataBinding.rlGameOptionB.setOnClickListener(this);
        mDataBinding.rlGame2OptionC.setOnClickListener(this);
        mDataBinding.rlGameOptionD.setOnClickListener(this);
        mDataBinding.ivGameLast.setOnClickListener(this);
        mDataBinding.ivGameNext.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()) {
            case R.id.rlGameOptionA://选项A
                isRight(1, mDataBinding.ivGameOptionAImg, mDataBinding.tvGameOptionA.getText().toString());
                break;
            case R.id.rlGameOptionB://选项B
                isRight(2, mDataBinding.ivGameOptionBImg, mDataBinding.tvGameOptionB.getText().toString());
                break;
            case R.id.rlGame2OptionC://选项C
                isRight(3, mDataBinding.ivGameOptionCImg, mDataBinding.tvGameOptionC.getText().toString());
                break;
            case R.id.rlGameOptionD://选项D
                isRight(4, mDataBinding.ivGameOptionDImg, mDataBinding.tvGameOptionD.getText().toString());
                break;
            case R.id.ivGameLast://上一关
                if (level == 0) {
                    ToastUtils.showShort(R.string.it_is_already_the_first_level);
                    return;
                }
                level--;
                setData(listAll.get(level));
                break;
            case R.id.ivGameNext://下一关
                if (level == (listAll.size() - 1)) {
                    ToastUtils.showShort(R.string.it_is_already_the_last_level);
                    return;
                }
                int spLevel = SPUtil.getInt(mContext, Content.GAME_NAME, 0);
                if (level == spLevel) {
                    ToastUtils.showShort(R.string.please_pass_now_level);
                    return;
                }
                level++;
                setData(listAll.get(level));
                break;
        }
    }

    @Override
    protected void initData() {
        listAll.clear();
        List<DmBean> dmBeans = DmDataProvider.getDmBeans();
        for (DmBean bean : dmBeans) {
            listAll.add(bean);
            listAnswer.add(bean.getName());
        }
        level = SPUtil.getInt(mContext, Content.GAME_NAME, 0);//获取对应难度的关卡进度
        setData(listAll.get(level));
    }

    //设置游戏数据
    private void setData(DmBean bean) {
        checkLevel();
        Glide.with(mContext).load(bean.getImgPath()).into(mDataBinding.ivGameImg);//图片
        List<String> list = RandomUtil.randomGetItems(listAnswer, bean.getName(), 3);
        mDataBinding.tvGameOptionA.setText(list.get(0));
        mDataBinding.tvGameOptionB.setText(list.get(1));
        mDataBinding.tvGameOptionC.setText(list.get(2));
        mDataBinding.tvGameOptionD.setText(list.get(3));
        boolean isA = checkAnswer(list.get(0), bean.getName());
        boolean isB = checkAnswer(list.get(1), bean.getName());
        boolean isC = checkAnswer(list.get(2), bean.getName());
        boolean isD = checkAnswer(list.get(3), bean.getName());
        if (isA) {
            rightNum = 1;
        } else if (isB) {
            rightNum = 2;
        } else if (isC) {
            rightNum = 3;
        } else if (isD) {
            rightNum = 4;
        }
    }

    //判断答案是否正确
    private boolean checkAnswer(String s1, String s2) {
        if (s1.equals(s2)) {
            return true;
        }
        return false;
    }

    //判断对错
    private void isRight(int kind, ImageView viewBg, String rightAnswer) {
        if (kind == rightNum) {
            viewBg.setImageResource(R.drawable.azhengq);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewBg.setImageResource(R.drawable.aweixuanz);
                    //判断是否需要解锁下一关
                    if (level < listAll.size()) {//如果当前关卡小于总关卡数
                        int spLevel = SPUtil.getInt(mContext, Content.GAME_NAME, 0);//获取记录通过关卡数
                        if (level == spLevel) {//如果通过的是未解锁关卡则解锁下一关
                            SPUtil.putInt(mContext, Content.GAME_NAME, spLevel + 1);
                        }
                    }
                    if (level == (listAll.size() - 1)) {
                        ToastUtils.showShort(R.string.it_is_already_the_last_level);
                        setData(listAll.get(level));
                        return;
                    }
                    level++;
                    setData(listAll.get(level));
                }
            }, 500);
        } else {
            viewBg.setImageResource(R.drawable.acuowu);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewBg.setImageResource(R.drawable.aweixuanz);
                }
            }, 500);
        }
    }

    //判断能否进入上一关和下一关
    private void checkLevel() {
        if (level == 0) {
            mDataBinding.ivGameLast.setImageResource(R.drawable.aqianjw);
        } else {
            mDataBinding.ivGameLast.setImageResource(R.drawable.aqianjingx);
        }
        mDataBinding.ivGameNext.setImageResource(R.drawable.ahoutuix);
        if (level == (listAll.size() - 1)) {
            mDataBinding.ivGameNext.setImageResource(R.drawable.ahoutuiwei);
        }
        int spLevel = SPUtil.getInt(mContext, Content.GAME_NAME, 0);
        if (level == spLevel) {
            mDataBinding.ivGameNext.setImageResource(R.drawable.ahoutuiwei);
        }
    }

}